package com.enfo.zjabank.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.support.DataAccessUtils;
import org.springframework.jdbc.core.ColumnMapRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapperResultSetExtractor;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * Created by zhoum on 2016/8/3.
 */
@Repository
public class BaseDao {
    @Autowired
    public JdbcTemplate jdbcTemplate;

    public Map<String,Object> queryForMap(String sql,Object[] params){
        List<Map<String,Object>> result = this.jdbcTemplate.query(sql,params,new RowMapperResultSetExtractor<Map<String, Object>>(new ColumnMapRowMapper(),1));
        return DataAccessUtils.uniqueResult(result);
    }

    public Map<String,Object> getUserInfo(String userCode){
        String sql = "select full_name,dept_code from org_user where user_code=?";
        return queryForMap(sql, new Object[]{userCode});
    }

    public <T> T queryForObject(String sql, Object[] args, Class<T> requiredType){
        try {
            return this.jdbcTemplate.queryForObject(sql,args,requiredType);
        } catch (DataAccessException e) {
            return null;
        }
    }
}
